package cn.iocoder.yudao.module.product.controller.admin.resourcecategory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.resourcecategory.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.resourcecategory.ResourceCategoryDO;
import cn.iocoder.yudao.module.product.service.resourcecategory.ResourceCategoryService;

@Tag(name = "管理后台 - 产品资源分享分类")
@RestController
@RequestMapping("/product/resource-category")
@Validated
public class ResourceCategoryController {

    @Resource
    private ResourceCategoryService resourceCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建产品资源分享分类")
    @PreAuthorize("@ss.hasPermission('product:resource-category:create')")
    public CommonResult<Integer> createResourceCategory(@Valid @RequestBody ResourceCategorySaveReqVO createReqVO) {
        return success(resourceCategoryService.createResourceCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品资源分享分类")
    @PreAuthorize("@ss.hasPermission('product:resource-category:update')")
    public CommonResult<Boolean> updateResourceCategory(@Valid @RequestBody ResourceCategorySaveReqVO updateReqVO) {
        resourceCategoryService.updateResourceCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品资源分享分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:resource-category:delete')")
    public CommonResult<Boolean> deleteResourceCategory(@RequestParam("id") Integer id) {
        resourceCategoryService.deleteResourceCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品资源分享分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:resource-category:query')")
    public CommonResult<ResourceCategoryRespVO> getResourceCategory(@RequestParam("id") Integer id) {
        ResourceCategoryDO resourceCategory = resourceCategoryService.getResourceCategory(id);
        return success(BeanUtils.toBean(resourceCategory, ResourceCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品资源分享分类分页")
    @PreAuthorize("@ss.hasPermission('product:resource-category:query')")
    public CommonResult<PageResult<ResourceCategoryRespVO>> getResourceCategoryPage(@Valid ResourceCategoryPageReqVO pageReqVO) {
        PageResult<ResourceCategoryDO> pageResult = resourceCategoryService.getResourceCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResourceCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品资源分享分类 Excel")
    @PreAuthorize("@ss.hasPermission('product:resource-category:export')")
    @OperateLog(type = EXPORT)
    public void exportResourceCategoryExcel(@Valid ResourceCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceCategoryDO> list = resourceCategoryService.getResourceCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品资源分享分类.xls", "数据", ResourceCategoryRespVO.class,
                        BeanUtils.toBean(list, ResourceCategoryRespVO.class));
    }

}